package com.wwwc.admin.web.servlet;

import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;

public class adminGenerateImageLink extends HttpServlet {

    public void doGet(HttpServletRequest request,  HttpServletResponse response)
	throws IOException, ServletException
    {
	doPost(request,response);
    }

    public void doPost(HttpServletRequest request, 
		       HttpServletResponse response)
	throws IOException, ServletException
    {
        response.setContentType("text/html");

	HttpSession session = request.getSession(true);
        PrintWriter out     = response.getWriter();
	String user_ip      = request.getRemoteAddr();

	String action    = request.getParameter("action");
	String options   = request.getParameter("options");

	if(action == null) {
	    out.println("<HTML><HEAD><TITLE></TITLE>");
	    out.println("</HEAD><BODY>");
	    out.println("<FORM method=post action adminGenerateImageList>");
	    out.println("<TABLE border=1 width=100%>");
	    out.println("<TR><TD align=center colspan=2>This is a utility for generate a image load html file</TD></TR>");
	    out.println("<TR><TD align=center>URL</TD>");
	    out.println("<TD><input type=text name=base_url size=90></TD></TR>");
	    out.println("<TR><TD align=center>Counter String</TD>");
	    out.println("<TD><input type=text name=counter_string size=90></TD></TR>");
	    out.println("<TR><TD align=center>Form:</TD>");
	    out.println("<TD><input type=text name=counter_from size=90></TD></TR>");
	    out.println("<TR><TD align=center>To:</TD>");
	    out.println("<TD><input type=text name=counter_to size=90></TD></TR>");
	    out.println("<TR><TD align=center>Image Type:</TD>");
	    out.println("<TD><input type=text name=image_type value='.jpg, .gif' size=90></TD></TR>");

	    out.println("<TR><TD></TD><TD><input type=submit name=B value=Submit>");
	    out.println("<input type=reset name=R value=Reset></TD></TR>");
	    out.println("<input type=hidden name=action value=gfile>");
	    out.println("</TABLE>");
	    out.println("</FORM>");
	    out.println("</BODY></HTML>");
	    return;
	}
	else if (action.equals("gfile")) {
	    String base_url       = request.getParameter("base_url");
	    String counter_string = request.getParameter("counter_string");
	    String counter_from   = request.getParameter("counter_from");
	    String counter_to     = request.getParameter("counter_to");
	    String image_type     = request.getParameter("image_type");

	    if(base_url==null || counter_from==null || 
	       counter_to==null || counter_string==null ||
	       image_type == null
	       ) {
		out.println("Try again!");
		return;
	    }
	    
	    File f = new File("C:/Documents and Settings/wli/My Documents/My Pictures","ImageLoad.html");

	    while(f.exists()) {
		f.delete();
		out.println("Deleting file:"+f.getPath()+"<BR>");
	    }
	    /*
	    out.println("<BR>1="+base_url);
	    out.println("<BR>2="+counter_string);
	    out.println("<BR>3="+counter_from);
	    out.println("<BR>4="+counter_to);
	    out.println("<BR>5="+image_type);
	    */
	    int s1 = Integer.parseInt(counter_from);
	    int s2 = Integer.parseInt(counter_to);
	    StringBuffer sbf = new StringBuffer();
	    for(int i = s1; i <= s2; i++) {
		String temp = "";
		String add_zero = "";
		if((i+"").length() != counter_from.length()) {
		    for(int n=1; n<= (counter_from.length()-(i+"").length()); n++) {
			add_zero += "0";
		    }

		}

		temp = add_zero+i+image_type;
		sbf.append("<img src="+base_url.replaceAll(counter_string,temp)+">");
	    }
	    out.println("<HTML><BODY>");
	    out.println(sbf);
	    out.println("</BODY></HTML>");
	}
    }
    
    private void gIndexFile(String title, String dir, String[] file_list) {

	File f = new File(dir,"index.html");
	while(f.exists()) {
	    f.delete();
	    f = new File(dir,"index.html");
	}

	try {
	    RandomAccessFile outputFile = new RandomAccessFile(f, "rw" );
	    outputFile.writeBytes("<HTML><HEAD><TITLE>index</TITLE>");
	    outputFile.writeBytes("<SCRIPT>");
	    outputFile.writeBytes("var db = new Array("+file_list.length+");");
	    for(int i=0; i<file_list.length;i++) {
		outputFile.writeBytes("db["+i+"]=\""+file_list[i]+"\";");
	    }

	    outputFile.writeBytes("function changeImage(m) {");
	    outputFile.writeBytes("    for(i=0;i<db.length;i++){");
	    outputFile.writeBytes("       if(document.pic.src.indexOf(\"/\"+db[i]) != -1){");
	    outputFile.writeBytes("          if(m==1) {");
	    outputFile.writeBytes("             if(i==(db.length-1)) {");
	    outputFile.writeBytes("                 i=-1;");
	    outputFile.writeBytes("             }");
	    outputFile.writeBytes("             document.pic.src=db[(i+1)];");
	    outputFile.writeBytes("             document.forms.f.counter.value=(i+1)+\"/\"+db.length;");
	    outputFile.writeBytes("          }");
	    outputFile.writeBytes("          if(m==-1) {");
	    outputFile.writeBytes("              if(i==0) {");
	    outputFile.writeBytes("                  i = db.length;");
	    outputFile.writeBytes("              }");
	    outputFile.writeBytes("              document.pic.src=db[(i-1)];");
	    outputFile.writeBytes("              document.forms.f.counter.value=(i-1)+\"/\"+db.length;");
	    outputFile.writeBytes("          }");
	    outputFile.writeBytes("          break;");
	    outputFile.writeBytes("       }");
	    outputFile.writeBytes("    }");
	    outputFile.writeBytes("}");

	    outputFile.writeBytes("function resizeImage() {");
	    outputFile.writeBytes("   var w = document.pic.width;");
	    outputFile.writeBytes("   var h = document.pic.height;");
	    outputFile.writeBytes("   while (w>700) {");
	    outputFile.writeBytes("     w = w*0.9;");
	    outputFile.writeBytes("   }");
	    outputFile.writeBytes("  document.pic.width  = w;");
	    outputFile.writeBytes("}");
	    outputFile.writeBytes("</SCRIPT>");
	    outputFile.writeBytes("</HEAD>");
	    outputFile.writeBytes("<BODY><CENTER>");
	    outputFile.writeBytes("<TABLE border=0 width=800 style='font-size:10pt'>");
	    outputFile.writeBytes("<TR><TD align=center>"+title+"</TD></TR>");
	    outputFile.writeBytes("</TABLE>");
	    //outputFile.writeBytes("<TR><TD align=center>");
	    outputFile.writeBytes("<FORM name=f>");
	    outputFile.writeBytes("<input type=button name=B1 value=Back style='font-size: 8pt' ");
	    outputFile.writeBytes(" onclick=javascript:changeImage(-1);>");
	    outputFile.writeBytes("<input type=button name=counter value=0/"+file_list.length);
	    outputFile.writeBytes(" style='font-size: 8pt'>");
	    outputFile.writeBytes("<input type=button name=B2 value=Next style='font-size: 8pt' ");
	    outputFile.writeBytes(" onclick=javascript:changeImage(1);>");
	    outputFile.writeBytes("</FORM>");
	    //outputFile.writeBytes("</TD></TR>");
	    //outputFile.writeBytes("</TABLE>");
	    outputFile.writeBytes("<TABLE border=1 width=800 height=500>");
	    outputFile.writeBytes("<TR><TD align=center>");
	    outputFile.writeBytes("<img name=pic src="+file_list[0]+" border=0");
	    outputFile.writeBytes(" onLoad=javascript:resizeImage();>");
	    outputFile.writeBytes("</TD></TR>");
	    
	    outputFile.writeBytes("</TABLE></CENTER></BODY></HTML>");
	    outputFile.close();
	}
	catch ( IOException e ) {

	}
    }

    private void gFrameIndexFile(String dir) {

	File f = new File(dir,"index_left.html");
	while(f.exists()) {
	    f.delete();
	    f = new File(dir,"index_left.html");
	}

	String dir_list[] = null;
	File   df         = new File (dir);
	dir_list = df.list();

	try {
	    RandomAccessFile outputFile = new RandomAccessFile(f, "rw" );
	    outputFile.writeBytes("<HTML><HEAD><TITLE>index_left</TITLE>");
	    outputFile.writeBytes("</HEAD>");
	    outputFile.writeBytes("<BODY>");
	    outputFile.writeBytes("<TABLE border=0 width=150 style='font-size:8pt'>");
	    
	    File sd = null;
	    for(int i=0; i<dir_list.length;i++) {
		sd = new File(dir,dir_list[i]);
		if(sd.isDirectory()) {
		    outputFile.writeBytes("<TR><TD align=left><A href=./"+dir_list[i]+"/index.html>Page["+(i+1)+"]</A></TD></TR>");
		}
	    }
	    
	    outputFile.writeBytes("</TABLE></BODY></HTML>");
	    outputFile.close();
	}
	catch ( IOException e ) {

	}
    }

    /*
	
       	String dir_id[] = null;
	StringBuffer sb = new StringBuffer ();
	Vector pro_v = new Vector();
	File f ;

	f = new File (inProfile);
	if (!f.isFile()) {
	    sb.append("<BR>Profile not found:");
	}
	else {
	    ProfileLoader loader = new ProfileLoader();
	    loader.setFileName(inProfile);
	    loader.loadProfile();
	    pro_v = loader.getVector();
	}

	f = new File (outHtmlfile);
	if (f.isFile()) {
	    f.delete();
	    sb.append("<BR>Delete:"+outHtmlfile);
	}
	else {
	    f = new File (outHtmlfile.substring(0, outHtmlfile.lastIndexOf("\\")));
	    if (!f.isDirectory()) {
		f.mkdirs();
	    }
	}

	String current_dir = inProfile.substring((inProfile.indexOf("htdocs\\")+6),inProfile.lastIndexOf("\\"));

	sb.append("<BR>current_dir="+current_dir);

	try {
	    RandomAccessFile outputFile = new RandomAccessFile( (outHtmlfile), "rw" );
	    outputFile.writeBytes("<HTML><HEAD><TITLE>Direcotry:["+current_dir+"] index</TITLE>");

	   
	    outputFile.writeBytes("</HEAD>\n");
	    outputFile.writeBytes("<BODY>\n");
	    outputFile.writeBytes("<TABLE border='0' width='300' height='100'>\n");
	    outputFile.writeBytes("<TR><TD><A href=/3wcg.html><IMG height ='100' width='100' border=0 ");
	    outputFile.writeBytes("src=/_system/images/3wc.JPG></A>\n");
	    outputFile.writeBytes("</TD></TR>\n</TABLE>\n<TABLE>\n");

	    outputFile.writeBytes("<TR><TD><a href=/index/servlet/SideBarEditServlet target=mainFrame>\n");
	    outputFile.writeBytes("<img border=0 src=/_system/images/edit.gif></a></TD></TR>");

	    Vector dir_v = new Vector();

	    if (current_dir.indexOf("\\") != -1) {

		StringTokenizer tokens = new StringTokenizer(current_dir,"\\");
		String temp = null;
		outputFile.writeBytes("<tr><td><A href=/_system/html/index_left.html>\n");
		outputFile.writeBytes(serverName.toUpperCase()+"</A></TD></TR>\n");
		
		while (tokens.hasMoreTokens()) {
		    temp = (String)tokens.nextToken();
		    dir_v.add(temp);
		}
	    }
		
	    for (int j=0; j<dir_v.size();j++) {

		outputFile.writeBytes("<tr><td><IMG src=/_system/images/L"+(j+1)+".gif>");
		
		if (j==(dir_v.size()-1)) {
		    outputFile.writeBytes(((String)dir_v.elementAt(j)).toUpperCase()+"</TD></TR>\n");
		}
		else {
		    outputFile.writeBytes("<A href=/"+dir_v.elementAt(j)+">\n");
		    outputFile.writeBytes(((String)dir_v.elementAt(j)).toUpperCase()+"</A></TD></TR>\n");
		}
	    }

	    outputFile.writeBytes("<TR><TD>\n");

	    if (addJsfile.equals("no")) {
		String    url_name   = null;
		String    url_adds   = null;

		try {
		    Enumeration enum = pro_v.elements();  
		    while (enum.hasMoreElements()) {
			url_name = (String)enum.nextElement();
			url_adds = (String)enum.nextElement();
			outputFile.writeBytes("<A href ="+url_adds+">"+url_name.toUpperCase()+"</A><BR>\n");
		    }
		}
		catch ( IOException io ) {
		}
	    }

	    if (addJsfile.equals("yes")) {
		outputFile.writeBytes("<SCRIPT SRC='/_system/javaScript/setCookie.js'></SCRIPT>");
		outputFile.writeBytes("<SCRIPT SRC='/_system/javaScript/sideBarStateHead.js'></SCRIPT>");
		outputFile.writeBytes("<SCRIPT SRC='./_system/javaScript/sideBarStateBody.js'></SCRIPT>");
		outputFile.writeBytes("<SCRIPT SRC='/_system/javaScript/sideBarStateEnd.js'></SCRIPT>");
		outputFile.writeBytes("<SCRIPT SRC='/_system/javaScript/sideBarDisplay.js'></SCRIPT>");
	    }
	    
	    outputFile.writeBytes("</TD></TR></TABLE></BODY></HTML>");
	    outputFile.close();
	    sb.append("<BR>File: "+outHtmlfile+" has been generated");
	}
	catch ( IOException e ) {
	}
	return(sb.toString());
    }

    private String[] getDirList(String dir)
    {
	File dirName = new File (dir);
	return(dirName.list());
    }
    */
}



